package com.yunkaweilai.android.view.popup;

import android.app.Activity;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.animation.Animation;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.google.gson.Gson;
import com.yunkaweilai.android.R;
import com.yunkaweilai.android.base.BaseApplication;
import com.yunkaweilai.android.constants.Api;
import com.yunkaweilai.android.interfaces.PopupDiscountSelector;
import com.yunkaweilai.android.interfaces.PopupShopSpecification;
import com.yunkaweilai.android.model.member.PreferentiaActivitiesModel;
import com.yunkaweilai.android.model.shop.ShopModel;
import com.yunkaweilai.android.model.shop.ShopSkuInfoModel;
import com.yunkaweilai.android.net.HttpRequest;
import com.yunkaweilai.android.net.PostProcess;
import com.yunkaweilai.android.utils.Utils;
import com.yunkaweilai.android.view.MyToast;
import com.zhy.adapter.abslistview.CommonAdapter;
import com.zhy.adapter.abslistview.ViewHolder;
import com.zhy.view.flowlayout.FlowLayout;
import com.zhy.view.flowlayout.TagAdapter;
import com.zhy.view.flowlayout.TagFlowLayout;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;

import razerdp.basepopup.BasePopupWindow;

/**
 * 商品规格弹窗
 * Created by Administrator on 2017/7/17.
 */

public class ShopSpecificationPopup extends BasePopupWindow{

    private View popupView;
    private Activity mContext;
    private PopupShopSpecification popupShopSpecification;
    private ShopModel.DataBean.ListBean model;
    private LayoutInflater mInflater;
    private ListView idListView;
    private CommonAdapter<ShopModel.DataBean.ListBean.SelectSpecToArrayBean> adapter;
    private TextView idTvSelector;
    private ShopSkuInfoModel skuInfoModel;
    private TextView idTvRight;
    private Map<String, ShopSkuInfoModel> selectedMaps;

    public ShopSpecificationPopup(Activity context, ShopModel.DataBean.ListBean model) {
        super(context);
        this.model = model;
        this.mContext = context;
        bindEvent();
    }
    public void setInterface(PopupShopSpecification popupShopSpecification){
        this.popupShopSpecification = popupShopSpecification;
    }

    @Override
    protected Animation initShowAnimation() {
        return getTranslateAnimation(250 * 2, 0, 300);
    }

    @Override
    public View getClickToDismissView() {
        return popupView.findViewById(R.id.id_img_close);
    }

    @Override
    public View onCreatePopupView() {
        popupView = LayoutInflater.from(getContext()).inflate(R.layout.popup_shop_specification, null);
        return popupView;
    }

    @Override
    public View initAnimaView() {
        return popupView.findViewById(R.id.id_rlayout_bottom);
    }

    private void bindEvent() {
        mInflater = LayoutInflater.from(mContext);
        selectedMaps = BaseApplication.getSelectedMaps();
        if (popupView != null) {
            initViews();
            initadapter();
            iniClick();
        }

    }

    private void iniClick() {
        idTvRight.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                ShopSkuInfoModel shopSkuInfoModel = selectedMaps.get(skuInfoModel.getSku());
                if (shopSkuInfoModel == null){
                    if (skuInfoModel.getMay_use_stock() > 0) {
                        popupShopSpecification.addSku(skuInfoModel,model.getGoods_category_parent_id(),true);
                        dismiss();
                    }else{
                        popupShopSpecification.addSku(skuInfoModel,model.getGoods_category_parent_id(),false);
                        dismiss();
                    }
                }else{
                    if (skuInfoModel.getMay_use_stock() > shopSkuInfoModel.getBuy_num()) {
                        popupShopSpecification.addSku(skuInfoModel,model.getGoods_category_parent_id(),true);
                        dismiss();
                    }else{
                        popupShopSpecification.addSku(skuInfoModel,model.getGoods_category_parent_id(),false);
                        dismiss();
                    }
                }

            }
        });
    }

    private void initadapter() {
        adapter = new CommonAdapter<ShopModel.DataBean.ListBean.SelectSpecToArrayBean>(mContext,R.layout.item_list_specification,model.getSelect_specToArray()) {
            @Override
            protected void convert(ViewHolder viewHolder, final ShopModel.DataBean.ListBean.SelectSpecToArrayBean item, int position) {
                viewHolder.setText(R.id.id_tv_name,item.getName());
                final TagFlowLayout tagFlowLayout = viewHolder.getView(R.id.id_tag_flow_layout);
                final TagAdapter<String> tagAdapter = new TagAdapter<String>(item.getChild()) {
                    @Override
                    public View getView(FlowLayout parent, int position, String o) {
                        TextView tv = (TextView) mInflater.inflate(R.layout.item_flow_tv,
                                tagFlowLayout, false);
                        tv.setText(o);
                        return tv;
                    }
                };
                tagFlowLayout.setAdapter(tagAdapter);

                tagAdapter.setSelectedList(item.getsPosition());
                tagFlowLayout.setOnSelectListener(new TagFlowLayout.OnSelectListener() {
                    @Override
                    public void onSelected(Set<Integer> selectPosSet) {
                        if (selectPosSet.size() == 0){
                            tagAdapter.setSelectedList(item.getsPosition());
                        }else{
                            item.setsPosition(selectPosSet.iterator().next());
                            idTvSelector.setText(selecotModel());
                        }
                        notifyDataSetChanged();
                    }
                });
            }
        };
        idListView.setAdapter(adapter);
        idTvSelector.setText(selecotModel());
    }


    private void initViews() {
        idListView = ((ListView) findViewById(R.id.id_listView));
        idTvSelector = ((TextView) findViewById(R.id.id_tv_selector));
        idTvRight = ((TextView) findViewById(R.id.id_tv_right));
    }
    //每次选择获取选中商品
    private String  selecotModel(){
        String strs = "";
        for (int i = 0; i < model.getSelect_specToArray().size(); i++) {
            ShopModel.DataBean.ListBean.SelectSpecToArrayBean selectSpecToArrayBean = model.getSelect_specToArray().get(i);
            strs = strs + selectSpecToArrayBean.getName() +":" + selectSpecToArrayBean.getChild().get(selectSpecToArrayBean.getsPosition()) + " ";
        }
        String md5Str = Utils.getMD5Str(strs);
        for (int i = 0; i < model.getSkuList().size(); i++) {
            if (md5Str.equals(model.getSkuList().get(i).getSpec_str_md5())){
                skuInfoModel = model.getSkuList().get(i);
            }
        }
        return skuInfoModel.getSku_name();
    }


}
